<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="用户昵称" prop="nick_name">
            <el-input v-model="form.nick_name" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="登录账号" prop="user_name">
            <el-input v-model="form.user_name" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">角色信息</h4>
    <el-table
      v-loading="loading"
      row-key="id"
      @row-click="clickRow"
      ref="tableRef"
      @selection-change="handleSelection"
      :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)">
      <el-table-column label="序号" width="55" type="index" align="center">
        <template #default="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55" />
      <el-table-column label="角色编号" align="center" prop="id" />
      <el-table-column label="角色名称" align="center" prop="name" />
      <el-table-column label="权限字符" align="center" prop="role_key" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="pageNum" v-model:limit="pageSize" />

    <div class="dialog-footer">
      <el-button class="medium" type="primary" :loading="submitting" :disabled="loading" @click="submitForm()"
        >提交</el-button
      >
      <el-button class="medium" @click="close()">返回</el-button>
    </div>
  </div>
</template>

<script setup name="AuthRole">
  import { infoAdminApi, updateAuthRoleApi } from '@/api/system/admin'
  import { selectRoleApi } from '@/api/system/role'

  const route = useRoute()
  const { proxy } = getCurrentInstance()

  const pageNum = ref(1)
  const pageSize = ref(10)

  const form = ref({
    id: undefined,
    nick_name: undefined,
    user_name: undefined,
    roleIds: []
  })

  // 初始化加载
  const loading = ref(true)
  onMounted(async () => {
    loading.value = true
    form.value.id = route.params?.adminId || ''
    if (!form.value.id) {
      proxy.$modal.msgWarning('参数缺失')
      close()
      return
    }
    await getRowData()
    await getRoleOptions()
    loading.value = false
  })

  // 获取信息
  async function getRowData() {
    try {
      const { info } = await infoAdminApi(form.value.id)
      form.value = proxy.dataToForm(form.value, info)
    } catch (error) {
      close()
    }
  }

  // 查询角色列表
  const roles = ref([])
  const total = ref(0)
  const tableRef = useTemplateRef('tableRef')
  async function getRoleOptions() {
    try {
      const { list } = await selectRoleApi()
      roles.value = list || []
      total.value = roles.value.length
      nextTick(() => {
        roles.value.forEach(row => {
          if (form.value.roleIds.includes(row.id)) {
            tableRef.value.toggleRowSelection(row, true)
          }
        })
      })
    } catch (e) {}
  }

  // 单击选中行数据
  async function clickRow(row) {
    tableRef.value.toggleRowSelection(row)
  }

  // 多选框选中数据
  const roleIds = ref([])
  function handleSelection(selection) {
    roleIds.value = selection.map(item => item.id)
  }

  // 关闭按钮
  function close() {
    const obj = { path: '/system/admin' }
    proxy.$tab.closeOpenPage(obj)
  }

  // 提交按钮
  const submitting = ref(false)
  function submitForm() {
    if (submitting.value) return
    submitting.value = true
    updateAuthRoleApi({ id: form.value.id, roleIds: roleIds.value.join(',') })
      .then(() => {
        proxy.$modal.msgSuccess('授权成功')
        close()
      })
      .catch(() => {
        submitting.value = false
      })
  }
</script>
